package com.example.flight;

import com.alibaba.fastjson.JSONObject;
import com.example.flight.generate.FlightBean;
import com.example.flight.generate.Source2Dest;
import com.example.flight.util.DateUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * @Author: leo
 * @date: 2021/7/2
 */
public class FlightService {

    public static void main(String[] args) throws IOException {
        List<Source2Dest> list = source2DestList();
        for (Source2Dest source2Dest : list) {
            String key = source2Dest.getDCity();
            String value = source2Dest.getACity();
            JSONObject jsonObject = httpPost(key,value);
            FlightBean flightBean = jsonObject.toJavaObject(FlightBean.class);
            FlightBean.ResponseBean response = flightBean.getResponse();
            if (response != null) {
                List<FlightBean.ResponseBean.DirectListBean> directList = response.getDirectList();
                Optional<FlightBean.ResponseBean.DirectListBean> first = directList.stream().filter(e -> {
                    FlightBean.ResponseBean.DirectListBean.FlightProfileBean flightProfile = e.getFlightProfile();
                    String dTime = flightProfile.getDTime();
                    try {
                        if (DateUtil.isEffectiveDate(dTime)) {
                            return true;
                        }
                    } catch (ParseException ex) {
                        ex.printStackTrace();
                    }
                    return false;
                }).findFirst();
                if (first.isPresent()){
                    FlightBean.ResponseBean.DirectListBean directListBean = first.get();
                    FlightBean.ResponseBean.DirectListBean.FlightProfileBean flightProfile = directListBean.getFlightProfile();
                    String discount = flightProfile.getDiscount();
                    String discountNo = new BigDecimal(discount).multiply(new BigDecimal(10)).stripTrailingZeros().toPlainString();
                    String price = flightProfile.getPrice();
                    System.out.println(key+"$$"+value+"$$"+discountNo+"$$"+price);
                }else {
                    System.out.println(key + "$$" + value + "$$" + "空" + "$$" + "空");
                }
            }

        }

    }



    public static JSONObject httpPost(String dCityName, String aCityName) throws IOException {
        String url = "https://ct.ctrip.com/restapi/restapi?_fxpcqlniredt=09031049415197996288";
        // 获取请求连接
        Connection con = Jsoup.connect(url).ignoreContentType(true);
        Map<String, String> cityCodeMap = getCityCodeMap();
        String jsonContent ="{\n" +
                "    \"serviceName\": \"CorpFltSearchService\",\n" +
                "    \"operation\": \"domesticSearch\",\n" +
                "    \"CorpPayType\": \"public\",\n" +
                "    \"params\": {\n" +
                "        \"channel\": \"ONLINE\",\n" +
                "        \"flightSearchInfo\": [\n" +
                "            {\n" +
                "                \"dDate\": \"2021-07-15\",\n" +
                "                \"dCity\": "+cityCodeMap.get(dCityName)+",\n" +
                "                \"aCity\": "+cityCodeMap.get(aCityName)+",\n" +
                "                \"dCityName\": "+dCityName+",\n" +
                "                \"aCityName\": "+aCityName+"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"policyUid\": \"\",\n" +
                "        \"tripType\": \"S\",\n" +
                "        \"isEmergency\": false,\n" +
                "        \"sort\": \"-1\",\n" +
                "        \"corpPayType\": \"PUB\",\n" +
                "        \"segNo\": 1,\n" +
                "        \"preMsgToken\": \"fc394677-3b89-45a1-a828-c9e99e5dfddf\",\n" +
                "        \"transactionID\": \"909eb6f7-b734-4bb0-9a75-ca64281f6e49\"\n" +
                "    },\n" +
                "    \"token\": \"3290d264-b3ce-4f56-a2d9-34a4aed7ba14\",\n" +
                "    \"sToken\": \"3290d264-b3ce-4f56-a2d9-34a4aed7ba14\",\n" +
                "    \"language\": \"CHS\",\n" +
                "    \"pu\": \"0.7156501339080437\",\n" +
                "    \"GE\": 1625207281567,\n" +
                "    \"Site\": \"1\",\n" +
                "    \"head\": {\n" +
                "        \"cid\": \"09031049415197996288\",\n" +
                "        \"ctok\": \"\",\n" +
                "        \"cver\": \"1.0\",\n" +
                "        \"lang\": \"01\",\n" +
                "        \"sid\": \"\",\n" +
                "        \"syscode\": \"09\",\n" +
                "        \"auth\": null,\n" +
                "        \"extension\": [\n" +
                "            {\n" +
                "                \"sourceFrom\": \"Online\"\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"contentType\": \"json\"\n" +
                "}";

        con.requestBody(jsonContent);
        // 插入cookie（头文件形式）
        String cookie = "_ga=GA1.2.1865223152.1625206593; IsFristShowInfo=2130126491; isShowCorpTip=yes; GUID=09031049415197996288; Hm_lvt_978e41b5a436a203cd8c50b1972f5e13=1625206593,1625464516; Hm_lpvt_978e41b5a436a203cd8c50b1972f5e13=1625464516; _gid=GA1.2.83717272.1625464516; Corp_ResLang=zh-cn; cticket=B5B63D43309E23DD7365076DF9383566D7C63A7D8977C88920C2671BACEAD499; ticket_ctrip=bJ9RlCHVwlu1ZjyusRi+ypZ7X2r4+yojyzYEMjGgdJUY+8MkQJWJwZJ1AGK2Pjd4zAAQPp94LF4w/WqKJcY6KhLA7ZzlVmewPYeuzGGgH5slsxtaZZAWLbSjUcH0V9iXnTOznLlWGde42bKwEizh867vmhvEwZbDS2byiflVv3WEGGEDtnx6+dsXpwjxSy2b/jyI1dxw9MeSaz+4hZEoHX4j09eNOn+7f0gcG7fxU8Gnwdg8/LcHsv/2WwKdMx5mz7gDOi6hZfo+4Qa5G8AqZLkx6YTPfQpmOaX5Ob8zL1k=; ASP.NET_SessionSvc=MTAuNjEuMjIuMjUwfDkwOTB8amlucWlhb3xkZWZhdWx0fDE2MjMxNDM5OTI3Mjg; SSOkey=17dcc10d-0e02-440d-99ab-412ccc31d3d2; _IsShowInfo=true; DUID=u=DC1A448F0309B3B22AE165F1E7445D13&v=0; sToken=130d3c759e1b49de9628eb58bc6cd0dc; _bfi=p1%3D10650036358%26p2%3D10650046610%26v1%3D10%26v2%3D9; _bfa=1.1625206593140.q9quf.1.1625214682801.1625464515895.3.11; _bfs=1.3";
        con.header("cookie", cookie);
        con.header(
                "user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36");
        Document doc = con.header("Content-Type", "application/json").post();
        Element body = doc.body();
        JSONObject json = JSONObject.parseObject(body.text());
        return json;
    }

    public static List<Source2Dest> source2DestList(){
        List<Source2Dest> list = new ArrayList<>();
        Source2Dest info ;
        info = new Source2Dest("南京",	"深圳");
        list.add(info);
        info = new Source2Dest("南宁",	"深圳");
        list.add(info);
        info = new Source2Dest("上海",	"深圳");
        list.add(info);
        info = new Source2Dest("深圳",	"北京");
        list.add(info);
        info = new Source2Dest("深圳",	"成都");
        list.add(info);
        info = new Source2Dest("深圳",	"赣州");
        list.add(info);
        info = new Source2Dest("深圳",	"贵阳");
        list.add(info);
        info = new Source2Dest("深圳",	"杭州");
        list.add(info);
        info = new Source2Dest("深圳",	"合肥");
        list.add(info);
        info = new Source2Dest("深圳",	"济南");
        list.add(info);
        info = new Source2Dest("深圳",	"昆明");
        list.add(info);
        info = new Source2Dest("深圳",	"临沂");
        list.add(info);
        info = new Source2Dest("深圳",	"南京");
        list.add(info);
        info = new Source2Dest("深圳",	"南宁");
        list.add(info);
        info = new Source2Dest("深圳",	"南通");
        list.add(info);
        info = new Source2Dest("深圳",	"宁波");
        list.add(info);
        info = new Source2Dest("深圳",	"青岛");
        list.add(info);
        info = new Source2Dest("深圳",	"上海");
        list.add(info);
        info = new Source2Dest("深圳",	"沈阳");
        list.add(info);
        info = new Source2Dest("深圳",	"石家庄");
        list.add(info);
        info = new Source2Dest("深圳",	"太原");
        list.add(info);
        info = new Source2Dest("深圳",	"天津");
        list.add(info);
        info = new Source2Dest("深圳",	"温州");
        list.add(info);
        info = new Source2Dest("深圳",	"武汉");
        list.add(info);
        info = new Source2Dest("深圳",	"西安");
        list.add(info);
        info = new Source2Dest("深圳",	"银川");
        list.add(info);
        info = new Source2Dest("深圳",	"长春");
        list.add(info);
        info = new Source2Dest("深圳",	"郑州");
        list.add(info);
        info = new Source2Dest("深圳",	"重庆");
        list.add(info);
        info = new Source2Dest("沈阳",	"成都");
        list.add(info);
        info = new Source2Dest("沈阳",	"深圳");
        list.add(info);
        info = new Source2Dest("南京",	"深圳");
        list.add(info);
        info = new Source2Dest("石家庄","深圳");
        list.add(info);
        info = new Source2Dest("太原",	"深圳");
        list.add(info);
        info = new Source2Dest("天津",	"深圳");
        list.add(info);
        info = new Source2Dest("温州",	"深圳");
        list.add(info);
        info = new Source2Dest("武汉",	"深圳");
        list.add(info);
        info = new Source2Dest("西安",	"深圳");
        list.add(info);
        info = new Source2Dest("银川",	"深圳");
        list.add(info);
        info = new Source2Dest("银川",	"重庆");
        list.add(info);
        info = new Source2Dest("长春",	"成都");
        list.add(info);
        info = new Source2Dest("长春",	"深圳");
        list.add(info);
        info = new Source2Dest("郑州",	"深圳");
        list.add(info);
        info = new Source2Dest("重庆",	"深圳");
        list.add(info);
        info = new Source2Dest("重庆",	"银川");
        list.add(info);
        return list;
    }

    public static Map<String,String> getDataMap(){
        Map<String,String> map = new HashMap<>();
        map.put("南京",	"深圳");
        map.put("南宁",	"深圳");
        map.put("上海",	"深圳");
        map.put("深圳",	"北京");
        map.put("深圳",	"成都");
        map.put("深圳",	"赣州");
        map.put("深圳",	"贵阳");
        map.put("深圳",	"杭州");
        map.put("深圳",	"合肥");
        map.put("深圳",	"济南");
        map.put("深圳",	"昆明");
        map.put("深圳",	"临沂");
        map.put("深圳",	"南京");
        map.put("深圳",	"南宁");
        map.put("深圳",	"南通");
        map.put("深圳",	"宁波");
        map.put("深圳",	"青岛");
        map.put("深圳",	"上海");
        map.put("深圳",	"沈阳");
        map.put("深圳",	"石家庄");
        map.put("深圳",	"太原");
        map.put("深圳",	"天津");
        map.put("深圳",	"温州");
        map.put("深圳",	"武汉");
        map.put("深圳",	"西安");
        map.put("深圳",	"银川");
        map.put("深圳",	"长春");
        map.put("深圳",	"郑州");
        map.put("深圳",	"重庆");
        map.put("沈阳",	"成都");
        map.put("沈阳",	"深圳");
        map.put("南京",	"深圳");
        map.put("石家庄","深圳");
        map.put("太原",	"深圳");
        map.put("天津",	"深圳");
        map.put("温州",	"深圳");
        map.put("武汉",	"深圳");
        map.put("西安",	"深圳");
        map.put("银川",	"深圳");
        map.put("银川",	"重庆");
        map.put("长春",	"成都");
        map.put("长春",	"深圳");
        map.put("郑州",	"深圳");
        map.put("重庆",	"深圳");
        map.put("重庆",	"银川");
        return map;
    }
    public static Map<String,String> getCityCodeMap(){
        Map<String,String > map = new HashMap<>();
        map.put("南宁" , "NNG");
        map.put("济南" , "TNA");
        map.put("昆明" , "KMG");
        map.put("临沂" , "LYI");
        map.put("赣州" , "KOW");
        map.put("宁波" , "NGB");
        map.put("重庆" , "CKG");
        map.put("石家庄" , "SJW");
        map.put("贵阳" , "KWE");
        map.put("长春" , "CGQ");
        map.put("哈尔滨" , "HRB");
        map.put("鞍山" , "AOG");
        map.put("合肥" , "HFE");
        map.put("天津" , "TSN");
        map.put("温州" , "WNZ");
        map.put("青岛" , "TAO");
        map.put("杭州" , "HGH");
        map.put("成都" , "CTU");
        map.put("上海" , "SHA");
        map.put("沈阳" , "SHE");
        map.put("南通" , "NTG");
        map.put("武汉" , "WUH");
        map.put("深圳" , "SZX");
        map.put("银川" , "INC");
        map.put("西安" , "SIA");
        map.put("郑州" , "CGO");
        map.put("太原" , "TYN");
        map.put("北京" , "BJS");
        map.put("南京" , "NKG");
        return map;
    }
}
